//index.js
//获取应用实例
// 子页面js加入: $ = app.globalData.$
var app = getApp(), $ = app.globalData.$;
Page({
  data: {
  },
  onLoad: function () {

    // 在当前页面onLoad函数中初始化jwxa
    $.jwxa(this);

  },
  setStorage: function () {
    // 存储数据
    var _userInfo = {
          id: 1,
          name: 'jwxa',
          age: 18,
          city: '深圳',
          mail: 'xujian_jason@163.com'
        },
        _userId = '1',
        _3rd_session = '1234567890',
        _token = '202cb962ac59075b964b07152d234b70';

    // 异步存储用户对象
    console.log('set account begin');
    $.storage('account', _userInfo, function () {
      console.log("account storage success");
    });
    console.log('set account end');
    // 异步存储token
    console.log('set token begin');
    $.storage({token: _token}, true);
    console.log('set token end');
    // 同步存储userId
    $.storage('userId', _userId);
    // 同步存储3rd_session( 传入对象存储 )
    $.storage({'3rd_session': _3rd_session});
  },
  getStorage: function () {
    // 异步获取account
    $.storage('account', function (res) {
      console.log("account info: ");
      console.log(res.data);
    });
    // 异步获取token
    $.storage('token', function (res) {
      console.log("token info: ");
      console.log(res.data);
    });
    // 同步获取3rd_session
    console.log("3rd_session = " + $.storage('3rd_session'));
    // 同步获取userId
    console.log("userId = " + $.storage('userId'));
  },
  removeStorage: function () {
    // 用户退出登录, 清理用户数据
    // 同步清理userId
    console.log('remove userId begin');
    $.removeStorage('userId');
    console.log('remove userId end');
    // 异步清理account
    console.log('remove account begin');
    $.removeStorage('account', function (res) {
      console.log("remove account success ");
    });
    console.log('remove account end');
  },
  clearStorage: function () {
    // 异步清理
    $.clearStorage();
    // 同步清理
    //$.clearStorage(true);
  }
})
